package edu.cmu.cs.cs214.hw9.client.controllers;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.cmu.cs.cs214.hw9.client.gui.FacelookScreen;
import edu.cmu.cs.cs214.hw9.datatypes.Status;

/**
 * A ListSelectionListener used for going to profiles when statuses are selected. Not a private class since more than one
 * screen uses status lists.
 */
public class StatusSelector implements ListSelectionListener {
	private FacelookScreen screen;
	
	public StatusSelector(FacelookScreen screen) {
		this.screen = screen;
	}
	
	/**
	 * Redirect the currently logged in user to the profile of the user who posted
	 * the status when the status is clicked on. 
	 */
	public void valueChanged(ListSelectionEvent e) {
		JList sourceList = (JList) e.getSource();
		Status status = (Status) sourceList.getSelectedValue();
		
		if (status != null) {
			screen.fireEventOccured(new FacelookEvent(Event.GO_TO_PROFILE, status.getPoster())); 
		}
	}
}
